﻿using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;
using HoneyBadgersOfficePlayer.Models;

namespace HoneyBadgersOfficePlayer.Views
{
    public partial class Register : System.Web.UI.Page
    {
        [BindableAttribute(true, BindingDirection.TwoWay)]
        [PersistenceModeAttribute(PersistenceMode.EncodedInnerDefaultProperty)]
        public virtual string Text { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
           
            //found this code doing a google search created by some smart guy(Laurent Duveau)... smarter than me...
            if (!Page.IsPostBack)
            {
                TextBox EULA_Textbox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("EULA_Textbox");
              EULA_Textbox.Text =
                      File.ReadAllText(Server.MapPath("eula.txt"));
            }
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {

            Guid userFKey;

            MembershipUser mu = Membership.GetUser(RegisterUser.UserName);
            userFKey = (Guid)mu.ProviderUserKey;

            TextBox addressBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Address");
            TextBox firstNameBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("First");
            TextBox lastNameBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Last");
            CheckBox eulaBox = (CheckBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("EULA_Check");
            TextBox cityBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("City");
            DropDownList stateBox = (DropDownList)RegisterUserWizardStep.ContentTemplateContainer.FindControl("State");
            TextBox zipBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Zip");
            CheckBox explicitBox = (CheckBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Explicit");
            

            string address = addressBox.Text;
            string firstName = firstNameBox.Text;
            string lastName = lastNameBox.Text;
            string city = cityBox.Text;
            string state = stateBox.Text;
            string zip = zipBox.Text;
            int expl = 0;
            if (explicitBox.Checked)
                expl = 1;

            bool eula = false;
            if (eulaBox.Checked)
                eula = true;


            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Login", RegisterUser.UserName));
            parameterList.Add(new SqlParameter("@Password", RegisterUser.Password));
            parameterList.Add(new SqlParameter("@Address", address));
            parameterList.Add(new SqlParameter("@First_Name", firstName));
            parameterList.Add(new SqlParameter("@Last_Name", lastName));
            parameterList.Add(new SqlParameter("@EULA_Agreement", eula));
            parameterList.Add(new SqlParameter("@User_Email", RegisterUser.Email));
            parameterList.Add(new SqlParameter("@Active", 1));
            parameterList.Add(new SqlParameter("@CityName", city));
            parameterList.Add(new SqlParameter("@StateName", state));
            parameterList.Add(new SqlParameter("@Zip_Code", zip));
            parameterList.Add(new SqlParameter("@explicit", expl));

            SqlParameter outputParameter = new SqlParameter("@User_ID", SqlDbType.Int);

            int userID = SQLUtilities.RunStoredProcedure("InsertNewUser", parameterList, outputParameter);
            Globals.UserID = userID;

            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);

           
        }

    }
}
